/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package demo.axitdn.event;

//import net.axitdn.control.*;

public class MultiEventListener implements
		KeyListener, FocusListener, PointerListener, ActionListener,
		CollisionListener, TouchListener {
	protected final EventListener a, b;

	protected MultiEventListener(EventListener a, EventListener b) {
		this.a = a;
		this.b = b;
	}

	protected static EventListener addInternal(EventListener a, EventListener b) {
		if (a == null)
			return b;
		if (b == null)
			return a;
		return new MultiEventListener(a, b);
	}

	protected static EventListener removeInternal(EventListener l,
			EventListener oldl) {
		if (l == oldl || l == null) {
			return null;
		} else if (l instanceof MultiEventListener) {
			return ((MultiEventListener) l).remove(oldl);
		} else {
			return l; // it's not here
		}
	}

	protected EventListener remove(EventListener oldl) {
		if (oldl == a)
			return b;
		if (oldl == b)
			return a;
		EventListener a2 = removeInternal(a, oldl);
		EventListener b2 = removeInternal(b, oldl);
		if (a2 == a && b2 == b) {
			return this; // it's not here
		}
		return addInternal(a2, b2);
	}

	public static KeyListener add(KeyListener a, KeyListener b) {
		return (KeyListener) addInternal(a, b);
	}

	public static FocusListener add(FocusListener a, FocusListener b) {
		return (FocusListener) addInternal(a, b);
	}

	public static KeyListener remove(KeyListener l, KeyListener oldl) {
		return (KeyListener) removeInternal(l, oldl);
	}

	public static FocusListener remove(FocusListener l, FocusListener oldl) {
		return (FocusListener) removeInternal(l, oldl);
	}

	public static ActionListener add(ActionListener l, ActionListener oldl) {
		return (ActionListener) addInternal(l, oldl);
	}

	public static ActionListener remove(ActionListener l, ActionListener oldl) {
		return (ActionListener) removeInternal(l, oldl);
	}

	public static CollisionListener add(CollisionListener l,
			CollisionListener oldl) {
		return (CollisionListener) addInternal(l, oldl);
	}

	public static CollisionListener remove(CollisionListener l,
			CollisionListener oldl) {
		return (CollisionListener) removeInternal(l, oldl);
	}
	public static TouchListener add(TouchListener l,
			TouchListener oldl) {
		return (TouchListener) addInternal(l, oldl);
	}

	public static TouchListener remove(TouchListener l,
			TouchListener oldl) {
		return (TouchListener) removeInternal(l, oldl);
	}

	/**/

	public void onKeyPressed(int keyCode, Object sender) {
		((KeyListener) a).onKeyPressed(keyCode, sender);
		((KeyListener) b).onKeyPressed(keyCode, sender);
	}

	public void onKeyReleased(int keyCode, Object sender) {
		((KeyListener) a).onKeyReleased(keyCode, sender);
		((KeyListener) b).onKeyReleased(keyCode, sender);
	}

	public void onKeyRepeated(int keyCode, int n, Object sender) {
		((KeyListener) a).onKeyRepeated(keyCode, n, sender);
		((KeyListener) b).onKeyRepeated(keyCode, n, sender);
	}

	public void onFocus(Object sender) {
		((FocusListener) a).onFocus(sender);
		((FocusListener) b).onFocus(sender);
	}

	public void onBlur(Object sender) {
		((FocusListener) a).onBlur(sender);
		((FocusListener) b).onBlur(sender);
	}

	public boolean onPointerPressed(int x, int y) {
		// throw new UnsupportedOperationException("Not supported yet.");
		return true;
	}

	public boolean onPointerDragged(int x, int y) {
		// throw new UnsupportedOperationException("Not supported yet.");
		return true;
	}

	public boolean onPointerReleased(int x, int y) {
		// throw new UnsupportedOperationException("Not supported yet.");
		return true;
	}

	public void actionPerformed(Object sender) {
		((ActionListener) a).actionPerformed(sender);
		((ActionListener) b).actionPerformed(sender);
	}

	public void onTouchedTopLeft(Object a, Object b) {
		((CollisionListener) a).onTouchedTopLeft(a, b);
		((CollisionListener) b).onTouchedTopLeft(a, b);
	}

	public void onTouchedTopRight(Object a, Object b) {
		((CollisionListener) a).onTouchedTopRight(a, b);
		((CollisionListener) b).onTouchedTopRight(a, b);
	}

	public void onTouchedDownLeft(Object a, Object b) {
		((CollisionListener) a).onTouchedDownLeft(a, b);
		((CollisionListener) b).onTouchedDownLeft(a, b);
	}

	public void onTouchedDownRight(Object a, Object b) {
		((CollisionListener) a).onTouchedDownRight(a, b);
		((CollisionListener) b).onTouchedDownRight(a, b);
	}

	public void onTouched(Object a, Object b) {
		((CollisionListener) a).onTouched(a, b);
		((CollisionListener) b).onTouched(a, b);
	}

	public boolean onPressed(int x, int y) {
		// TODO Auto-generated method stub
		((TouchListener) a).onPressed(x,y);
		((TouchListener) b).onPressed(x,y);
		return false;
	}

	public boolean onDragged(int x, int y) {
		// TODO Auto-generated method stub
		((TouchListener) a).onDragged(x,y);
		((TouchListener) b).onDragged(x,y);
		return false;
	}

	public boolean onReleased(int x, int y) {
		// TODO Auto-generated method stub
		((TouchListener) a).onReleased(x,y);
		((TouchListener) b).onReleased(x,y);
		return false;
	}

}
